<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-05-28
 * Time: 09:53
 * 省份管理类
 */

namespace app\api\controller\v1;

use app\api\model\MemberCost;
use think\facade\Request;

class Cost
{
    /**
     * 获取全部
     */
    public function getDataAll()
    {
        return MemberCost::order('id asc')
                ->select()->toArray();
    }
    /**
     * 获取列表
     * @validate('PagingParameter')
     * @auth('会员费列表','会员管理')
     */
    public function getDataList($page,$limit,$name)
    {
        if($name) {
            $query = MemberCost::where('name','like','%'.$name.'%')
                ->order('id asc');
        } else {
            $query = MemberCost::order('id asc');
        }
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 1) {
                $item['status_name'] = '一天';
            }elseif($item['status'] == 2) {
                $item['status_name'] = '一周';
            }elseif($item['status'] == 3) {
                $item['status_name'] = '一个月';
            }elseif($item['status'] == 4) {
                $item['status_name'] = '半年';
            }elseif($item['status'] == 5) {
                $item['status_name'] = '一年';
            }elseif($item['status'] == 6) {
                $item['status_name'] = '两年';
            }elseif($item['status'] == 7) {
                $item['status_name'] = '三年';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return MemberCost::where('id',$id)->find();
    }

    /**
     * 创建分类
     * @validate('CheckCost')
     */
    public function createData()
    {
        $data = Request::post();
        MemberCost::create($data);
        return writeJson(201, [], '添加成功！');
    }

    /**
     * 更新信息
     * @validate('CheckCost')
     */
    public function updateData()
    {
        $data = Request::put();
        $mode = new MemberCost();
        $mode->allowField(true)->save($data,['id',$data['id']]);
        return writeJson(202, [], '修改成功！');
    }

    /**
     * 删除信息
     * @validate('IdMustBePositiveInt')
     */
    public function deleteData($id)
    {
        MemberCost::destroy($id);
        return writeJson(203, [], '删除成功！');
    }
}